package com.wenbiao.springcloud.hystrix;


import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;

/**
 * 自定义Hystrix请求
 *
 */
public class MyHystrixCommand extends HystrixCommand<String> {

    private RestTemplate restTemplate;

    public MyHystrixCommand(Setter setter,RestTemplate restTemplate){
        super(setter);
        this.restTemplate=restTemplate;
    }

    @Override
    protected String run() throws Exception {
        //调用远程服务
        restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/hello", String.class).getBody();


        return null;
    }

    /**
     * 当远程服务超时、异常、不可用时、会触发当熔断方法
     * @return
     */
    @Override
    public String getFallback(){
        Throwable throwable=super.getExecutionException();
        System.out.println(throwable.getMessage());
        System.out.println(throwable.getStackTrace());
        //实现服务熔断降级

        return "error";
    }
}
